<?php

/**
 * Manage some base actions
 * @author Trung Tran
 */
class CustomerController extends Controller {
    public function filters(){
        return array(
            'accessControl', // perform access control for CRUD operations
            'postOnly + post,register,login', // we only allow deletion via POST request
        );
    }

    /**
     * register an user
     *
     * @var string username
     * @var string password
     * @var string first_name
     * @var string last_name
     * @var string email_name
     * @var string phone_number
     *
     * @return json
     */
    public function actionRegister(){
        $customer = new Customer();
        $customer->username = $_POST['username'];
        $customer->password = $_POST['password'];
        $customer->first_name = $_POST['first_name'];
        $customer->last_name = $_POST['last_name'];
        $customer->email = $_POST['email'];
        $customer->phone_number = $_POST['phone_number'];

        if($customer->save()){
            $this->toJSON(array(
                'status' => true
            ));
        }else{
            $this->toJSON(array(
                'status' => false,
                'errors' => $customer->getErrors()
            ));
        }
    }

    /**
     * login
     *
     * @var string username
     * @var string password
     */
    public function actionLogin(){
        if(isset($_POST['username'])){
            $customer = Customer::model()->findByAttributes(array('username'=>$_POST['username']));

            if(!$customer){
                $this->toJSON(array(
                    'status' => false,
                    'errors' => 'Invalid username'
                ));
            } elseif(!$customer->validatePassword($_POST['password'])){
                $this->toJSON(array(
                    'status' => false,
                    'errors' => 'Wrong password'
                ));
            }else{
                $session = Session::model()->findByAttributes(array('customer_id'=>$customer->id)) ;
                if(!$session){
                    $session = new Session();
                    $session->customer_id = $customer->id;
                    $session->created = time();
                    $session->token = uniqid();
                    $session->device_token = uniqid();
                    $session->device_type = 'IOS';
                    if(!$session->save()){
                        echo $session->getErrors();
                    }
                }
                $this->toJSON(array(
                    'status' => true,
                    'token' => $session->token ,
                    'device_token' => $session->device_token ,
                    'device_type' => $session->device_type ,
                ));
            }
        }
    }

    /**
     * verify code
     *
     * @var verify_code
     * @var id
     *
     */
    public function actionVerify($code,$id){
        $customer = Customer::model()->findByAttributes(array('verify_code'=> $code,'id' => $id));
        if (!$customer){
            $this->toJSON(array(
                'status' => false,
                'message' => 'Active fail'
            ));
        } else {
            $customer->saveAttributes(array('status' => 1));
            $this->toJSON(array(
                'status' => true,
                'message' => 'Active success'
            ));
        }
    }


}